package ro.toporjinschi.anytaxi.database;

import ro.toporjinschi.anytaxi.database.lists.PlacesList;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.ProgressBar;

public class DatabaseAccess extends SQLiteOpenHelper {

    private ProgressBar progress;
    private int startValue;
    private int stopValue;

    public DatabaseAccess(Context context, String dbName, int dbVersion) {
        super(context, dbName, null, dbVersion);
    }

    public ProgressBar getProgress() {
        return this.progress;
    }

    public void setProgress(ProgressBar progress, int startValue, int stopValue) {
        this.progress = progress;
        this.startValue = startValue;
        this.stopValue = stopValue;
    }

    protected void increaseProgress(int i) {
        if (this.progress != null) {
            this.progress.setProgress(i);
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        increaseProgress(this.startValue);
        db.execSQL(TablesActions.CREATE_PLACES);
        increaseProgress(this.stopValue);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        increaseProgress(this.startValue);
        db.execSQL(TablesActions.DROP_PLACES);
        increaseProgress((this.startValue - this.stopValue) / 2);
        db.execSQL(TablesActions.CREATE_PLACES);
        increaseProgress(this.stopValue);
    }

    public PlacesList places() {
        return new PlacesList(this);
    }
}